//
//  MapViewController.m
//  FreeTravel
//
//  Created by 徐康 on 16/9/13.
//  Copyright © 2016年 KX. All rights reserved.
#import "ReGeocodeAnnotation.h"
#import "POIAnnotation.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import "MapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import "ModelOfCityMap.h"
#import "ModelOfCityServicePoi_list.h"
@interface MapViewController ()<AMapLocationManagerDelegate,AMapSearchDelegate,MAMapViewDelegate>
@property (nonatomic, strong)MAMapView *mapView;
@property (nonatomic, strong)AMapLocationManager *locationManager;
@property (nonatomic, strong)AMapSearchAPI *search;
@property (nonatomic, assign) BOOL isSearchFromDragging;
@property (nonatomic, strong) ReGeocodeAnnotation *annotation;
@property (nonatomic, assign) NSInteger integer;
@end

@implementation MapViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%@",self.array);
    
    // Do any additional setup after loading the view.
    ///初始化地图
    self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    // 不显示罗盘
    _mapView.showsCompass = NO;
    //    // 显示比例尺
    _mapView.showsScale = YES;
    // 地图缩放等级
//    _mapView.zoomLevel = 16;
    // 开启定位
//    _mapView.showsUserLocation = YES;
    // 追踪用户位置.
//     self.mapView.userTrackingMode = MAUserTrackingModeFollow;
    ///把地图添加至view
    _mapView.delegate = self;
    [self.view addSubview:_mapView];
    
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    if (self.array.count>0) {
        for (int i = 0; i < self.array.count; i++) {
            ModelOfCityMap *model = self.array[i];
            
            AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
            regeo.location  = [AMapGeoPoint locationWithLatitude:model.lat longitude:model.lng];
            
            regeo.requireExtension = YES;
            [self.search AMapReGoecodeSearch:regeo];
        }
    }
    
    if (self.arrOfService.count >0) {
        for (int i = 0; i < self.arrOfService.count; i++) {
            ModelOfCityServicePoi_list *model = self.arrOfService[i];
            
            AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
            regeo.location  = [AMapGeoPoint locationWithLatitude:model.lat longitude:model.lng];
            
            regeo.requireExtension = YES;
            [self.search AMapReGoecodeSearch:regeo];
        }
    }
    

    
    
    
    
    

}
- (void)searchReGeocodeWithCoordinate:(CLLocationCoordinate2D)coordinate
{
    AMapReGeocodeSearchRequest *regeo = [[AMapReGeocodeSearchRequest alloc] init];
    
    regeo.location                    = [AMapGeoPoint locationWithLatitude:coordinate.latitude longitude:coordinate.longitude];
    regeo.requireExtension            = YES;
    
    [self.search AMapReGoecodeSearch:regeo];
}
/* 逆地理编码回调. */
- (void)onReGeocodeSearchDone:(AMapReGeocodeSearchRequest *)request response:(AMapReGeocodeSearchResponse *)response
{
    if (response.regeocode != nil && _isSearchFromDragging == NO)
    {
        CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake(request.location.latitude, request.location.longitude);
        ReGeocodeAnnotation *reGeocodeAnnotation = [[ReGeocodeAnnotation alloc] initWithCoordinate:coordinate
                                                                                         reGeocode:response.regeocode];
        
        [self.mapView addAnnotation:reGeocodeAnnotation];
        [self.mapView selectAnnotation:reGeocodeAnnotation animated:YES];
    }
    else /* from drag search, update address */
    {
        [self.annotation setAMapReGeocode:response.regeocode];
        [self.mapView selectAnnotation:self.annotation animated:YES];
    }
}
#pragma mark - MAMapViewDelegate

- (void)mapView:(MAMapView *)mapView didLongPressedAtCoordinate:(CLLocationCoordinate2D)coordinate
{
    _isSearchFromDragging = NO;
    [self searchReGeocodeWithCoordinate:coordinate];
}
#pragma mark - 移动视角
//- (void)mapView:(MAMapView *)mapView annotationView:(MAAnnotationView *)view didChangeDragState:(MAAnnotationViewDragState)newState fromOldState:(MAAnnotationViewDragState)oldState
//{
//    if (newState == MAAnnotationViewDragStateEnding)
//    {
//        _isSearchFromDragging = YES;
//        
//        CLLocationCoordinate2D coordinate = view.annotation.coordinate;
//        self.annotation = view.annotation;
//        
//        [self searchReGeocodeWithCoordinate:coordinate];
//    }
//}
- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view{
    NSArray * array = [NSArray arrayWithArray:_mapView.annotations];
//    if (view.selected == YES) {
//         view.image = [UIImage imageNamed:@"景点"];
//    }
    for (int i=0; i<array.count; i++)
        
    {
        
        if (view.annotation.coordinate.latitude ==((MAPointAnnotation*)array[i]).coordinate.latitude && view.annotation.coordinate.longitude ==((MAPointAnnotation*)array[i]).coordinate.longitude)
            
        {
            
            //获取到当前的大头针  你可以执行一些操作
            
            view.canShowCallout= YES;       //设置气泡可以弹出，默认为NO
            view.image = [UIImage imageNamed:@"景点"];
//            view.highlighted = YES;
            view.draggable = NO;        //设置标注可以拖动，默认为NO
            self.integer = i;
        }
        
        else
            
        {
            
            //对其余的大头针进行操作  我是删除
            
            //[_mapView removeAnnotation:array[i]];
            
            
        }
        
    }
}
// 检索失败
- (void)AMapSearchRequest:(id)request didFailWithError:(NSError *)error
{
    NSLog(@"Error: %@", error);
}
- (void)setArray:(NSMutableArray *)array{
    _array = array;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
